package ED2;

import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public final class View extends DirectedSparseMultigraph<Vertex, Edge>
{
    private ManipulaDados dataManager;
    private HashMap<Integer, Vertex> vertexes;
    private HashMap<String, Edge> edges;
    
    public View()
    {
        dataManager = new ManipulaDados();
        vertexes = new HashMap<Integer, Vertex>();
        edges = new HashMap<String, Edge>();
        
        ArrayList<AlunoBean> allAlunos = dataManager.getAllAlunos();
        
        for(AlunoBean aluno : allAlunos)
        {
            vertexes.put(aluno.getRm(), new Vertex(aluno));
        }
        
        build();
    }
    
    public HashMap<Integer, Vertex> getVertexes()
    {
        return vertexes;
    }
    
    public HashMap<String, Edge> getGraphEdges()
    {
        return edges;
    }
    
    private void build()
    {
        for(Map.Entry<Integer, Vertex> entry : vertexes.entrySet())
        {
            Vertex vertex = entry.getValue();

            addVertex(vertex);
            
            for(int amigoRm : vertex.getvizinhoRm())
            {
                Edge edge = new Edge(vertex, vertexes.get(amigoRm));
                
                addEdge(edge, vertexes.get(vertex.getRm()), vertexes.get(amigoRm));
                
                edges.put(generateEdgeKey(edge.getCodigoAluno().getRm(), edge.getTargetAluno().getRm()), edge);
            }
        }
    }
    
    public String generateEdgeKey(int sourceRm, int targetRm)
    {
        return "" + sourceRm + "" + targetRm;
    }
}
